from pprint import pprint

from django import http
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.paginator import Paginator, EmptyPage
from django.http import HttpResponse
# from django.shortcuts import render
from django.shortcuts import render
from django.views import View
from utils import constants
# from apps.goods.models import GoodsChannel
# from apps.goods.utils import get_fenlei
# from apps.index.models import ContentCategory
from apps.news.models import News
from apps.user.models import User


class IndexView(View):
    def get(self, request):
        """
        主页
        :param request:
        :return:
        """
        sort = request.GET.get('sort', 'newest')
        if sort == "heated":
            # 按照评论量排序
            sort_field = '-comment_num'
        else:
            # 'price'和'sales'以外的所有排序方式都归为'default'
            sort = 'newest'
            sort_field = '-create_time'

        news = News.objects.all().order_by(sort_field)
        new = News.objects.all().order_by("-comment_num")[:5]
        print(new)
        # 创建分页器：每页N条记录
        paginator = Paginator(news, 10)
        # 获取每页商品数据
        try:
            page_nums = request.GET.get("page")
            # print(page_nums)
            page_num = int(page_nums)
        except:
            page_num = 1
        try:
            page_skus = paginator.page(page_num)
            # print(page_skus)
        except EmptyPage:
            # 如果page_num不正确，默认给用户404
            return http.HttpResponseNotFound('empty page')
        # 获取列表页总页数
        total_page = paginator.num_pages
        # print(news)
        user = request.user
        if str(user) == "AnonymousUser":
            context = {
                'new': new,
                'num': 1,
                'sort': sort,  # 排序字段
                'page_skus': page_skus,  # 分页后数据
                'total_page': total_page,  # 总页数
                'page_num': page_num,  # 当前页码
            }
        else:
            context = {
                'new': new,
                'num': 1,
                'user': user,
                'sort': sort,  # 排序字段
                'page_skus': page_skus,  # 分页后数据
                'total_page': total_page,  # 总页数
                'page_num': page_num,  # 当前页码
            }

        return render(request, 'index.html', context=context)


class IndexZyView(View):
    def get(self, request):
        """
        主页
        :param request:
        :return:
        """
        are = request.GET.get('are', 'tw')
        num = 1
        if are == "fx":
            num = 2
        elif are == "tl":
            num = 3
        sort = request.GET.get('sort', 'newest')
        if sort == "heated":
            # 按照评论量排序
            sort_field = 'comment_num'
        else:
            # 'price'和'sales'以外的所有排序方式都归为'default'
            sort = 'newest'
            sort_field = '-create_time'
        new = News.objects.all().order_by("-comment_num")[:5]
        news = News.objects.filter(category=num).order_by(sort_field)
        # 创建分页器：每页N条记录
        paginator = Paginator(news, 10)
        # 获取每页商品数据
        try:
            page_nums = request.GET.get("page")
            page_num = int(page_nums)
        except:
            page_num = 1
        try:
            page_skus = paginator.page(page_num)
        except EmptyPage:
            # 如果page_num不正确，默认给用户404
            return http.HttpResponseNotFound('empty page')
        # 获取列表页总页数
        total_page = paginator.num_pages
        # print(news)
        user = request.user
        print(page_skus)
        if str(user) == "AnonymousUser":
            context = {
                "new": new,
                'num': 1,
                'sort': sort,  # 排序字段
                'page_skus': page_skus,  # 分页后数据
                'total_page': total_page,  # 总页数
                'page_num': page_num,  # 当前页码
            }
        else:
            context = {
                "new": new,
                'num': are,
                'user': user,
                'sort': sort,  # 排序字段
                'page_skus': page_skus,  # 分页后数据
                'total_page': total_page,  # 总页数
                'page_num': page_num,  # 当前页码
            }

        return render(request, 'index.html', context=context)


class IndexSView(LoginRequiredMixin, View):
    def get(self, request):
        """
        主页
        :param request:
        :return:
        """
        are = request.GET.get('q')
        print(are, "*" * 20)
        new = News.objects.all().order_by("-comment_num")[:5]
        news = News.objects.filter(title__icontains=are)
        print(news)
        # 创建分页器：每页N条记录
        paginator = Paginator(news, 10)
        # 获取每页商品数据
        try:
            page_nums = request.GET.get("page")
            page_num = int(page_nums)
        except:
            page_num = 1
        try:
            page_skus = paginator.page(page_num)
        except EmptyPage:
            # 如果page_num不正确，默认给用户404
            return http.HttpResponseNotFound('empty page')
        # 获取列表页总页数
        total_page = paginator.num_pages
        # print(news)
        user = request.user
        print(page_skus)
        if str(user) == "AnonymousUser":
            context = {
                "new": new,
                'num': "search",
                'page_skus': page_skus,  # 分页后数据
                'total_page': total_page,  # 总页数
                'page_num': page_num,  # 当前页码
                'are': are
            }
        else:
            context = {
                "new": new,
                'num': "search",
                'user': user,
                'page_skus': page_skus,  # 分页后数据
                'total_page': total_page,  # 总页数
                'page_num': page_num,  # 当前页码
                'are': are
            }

        return render(request, 'index.html', context=context)
